//
//  SmallDecisionsSjsSetViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/25.
//

#import "SmallDecisionsSjsSetViewController.h"
#import "SmallDecisionsSwitchView.h"
#import "SmallDecisionsSjsViewController.h"
#import "SmallDecisionsTextField.h"

@interface SmallDecisionsSjsSetViewController ()

@property(nonatomic,strong)UIView *containerView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIButton *sureBtn;
@property (nonatomic, strong) SmallDecisionsTextField *minTextField;
@property (nonatomic, strong) SmallDecisionsTextField *maxTextField;
@property (nonatomic, strong) SmallDecisionsTextField *numTextField;
@property (nonatomic, strong) SmallDecisionsSwitchView *switchView;

@end

@implementation SmallDecisionsSjsSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"随机数";
    [self containerView];
    [self titleLabel];
    [self minTextField];
    [self maxTextField];
    [self numTextField];
    [self switchView];
    [self sureBtn];
}

- (UIView *)containerView {
    if(!_containerView) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = SmallDecisionsPurpleColor;
        _containerView.layer.cornerRadius = 10;
        _containerView.layer.masksToBounds = YES;
        [self.view addSubview:_containerView];
        [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(30);
            make.right.mas_equalTo(-30);
            make.centerY.mas_equalTo(0);
        }];
    }
    return _containerView;
}

- (UILabel *)titleLabel {
    if(!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = SmallDecisionsYellowColor;
        _titleLabel.text = @"随机数设置";
        _titleLabel.font = SmallDecisionsSetBoldFontSize(22);
        _titleLabel.textAlignment = 1;
        [self.containerView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(20);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
    }
    return _titleLabel;
}

- (SmallDecisionsTextField *)minTextField {
    if(!_minTextField) {
        _minTextField = [[SmallDecisionsTextField alloc] init];
        _minTextField.textColor = SmallDecisionsThemeFontColor;
        _minTextField.backgroundColor = SmallDecisionsSecondBgColor;
        _minTextField.layer.cornerRadius = 6;
        _minTextField.layer.masksToBounds = YES;
        _minTextField.textEdgeInset = UIEdgeInsetsMake(0, 15, 0, 0);
        _minTextField.leadingViewRect = CGRectMake(0, 0, 80, 56);
        _minTextField.trailingViewRect = CGRectMake(0, 0, 10, 50);
        _minTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_minTextField.leadingViewBtn setTitle:@"最小值" forState:UIControlStateNormal];
        _minTextField.leadingViewBtn.titleLabel.font = SmallDecisionsSetFontSize(16);
        [_minTextField.leadingViewBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        _minTextField.textAlignment = 2;
        _minTextField.placeholder = @"最小值";
        [self.containerView addSubview:_minTextField];
        [_minTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(20);
            make.height.mas_equalTo(56);
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
        }];
    }
    return _minTextField;
}

- (SmallDecisionsTextField *)maxTextField {
    if(!_maxTextField) {
        _maxTextField = [[SmallDecisionsTextField alloc] init];
        _maxTextField.textColor = SmallDecisionsThemeFontColor;
        _maxTextField.placeholder = @"最大值";
        _maxTextField.backgroundColor = SmallDecisionsSecondBgColor;
        _maxTextField.layer.cornerRadius = 6;
        _maxTextField.layer.masksToBounds = YES;
        _maxTextField.textEdgeInset = UIEdgeInsetsMake(0, 15, 0, 0);
        _maxTextField.leadingViewRect = CGRectMake(0, 0, 80, 56);
        _maxTextField.trailingViewRect = CGRectMake(0, 0, 10, 50);
        _maxTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_maxTextField.leadingViewBtn setTitle:@"最大值" forState:UIControlStateNormal];
        _maxTextField.leadingViewBtn.titleLabel.font = SmallDecisionsSetFontSize(16);
        [_maxTextField.leadingViewBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        _maxTextField.textAlignment = 2;
        [self.containerView addSubview:_maxTextField];
        [_maxTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.minTextField.mas_bottom).offset(20);
            make.left.right.height.mas_equalTo(self.minTextField);
        }];
    }
    return _maxTextField;
}
- (SmallDecisionsTextField *)numTextField {
    if(!_numTextField) {
        _numTextField = [[SmallDecisionsTextField alloc] init];
        _numTextField.textColor = SmallDecisionsThemeFontColor;
        _numTextField.placeholder = @"数量";
        _numTextField.backgroundColor = SmallDecisionsSecondBgColor;
        _numTextField.layer.cornerRadius = 6;
        _numTextField.layer.masksToBounds = YES;
        _numTextField.textEdgeInset = UIEdgeInsetsMake(0, 15, 0, 0);
        _numTextField.leadingViewRect = CGRectMake(0, 0, 80, 56);
        _numTextField.trailingViewRect = CGRectMake(0, 0, 10, 50);
        _numTextField.keyboardType = UIKeyboardTypeNumberPad;
        [_numTextField.leadingViewBtn setTitle:@"数量" forState:UIControlStateNormal];
        _numTextField.leadingViewBtn.titleLabel.font = SmallDecisionsSetFontSize(16);
        [_numTextField.leadingViewBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        _numTextField.textAlignment = 2;
        [self.containerView addSubview:_numTextField];
        
        [_numTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.maxTextField.mas_bottom).offset(20);
            make.left.right.height.mas_equalTo(self.maxTextField);
        }];
    }
    return _numTextField;
}

- (SmallDecisionsSwitchView *)switchView {
    if(!_switchView) {
        _switchView = [[SmallDecisionsSwitchView alloc] init];
        _switchView.leftLabel.text = @"允许重复";
        _switchView.layer.cornerRadius = 6;
        _switchView.layer.masksToBounds = YES;
        _switchView.backgroundColor = SmallDecisionsSecondBgColor;
        [self.containerView addSubview:_switchView];
        [_switchView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.numTextField.mas_bottom).offset(20);
            make.left.right.height.mas_equalTo(self.numTextField);
        }];
    }

    return _switchView;
}

- (UIButton *)sureBtn {
    if(!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor clearColor] forState:UIControlStateHighlighted];
        _sureBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(14);
        _sureBtn.layer.cornerRadius = 23;
        _sureBtn.layer.masksToBounds = YES;
        _sureBtn.backgroundColor = SmallDecisionsThemeColor;
        [_sureBtn addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:_sureBtn];
        [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(46);
            make.left.right.equalTo(self.switchView);
            make.top.equalTo(self.switchView.mas_bottom).offset(30);
            make.bottom.mas_offset(-30);
        }];
    }
    return _sureBtn;
}

- (void)sureBtnAction {
    NSInteger min = [self.minTextField.text intValue];
    NSInteger max = [self.maxTextField.text intValue];
    NSInteger count = [self.numTextField.text intValue];
    BOOL allowDuplicates = self.switchView.rightSwitch.isOn;
    // 验证参数有效性
    if (min > max) {
        [SmallDecisionsHUDHelper showError:@"最小值不能大于最大值"];
        return;
    }

    if (count <= 0) {
        [SmallDecisionsHUDHelper showError:@"生成数量必须大于0"];
        return;
    }

    if (!allowDuplicates && (max - min + 1) < count) {
        [SmallDecisionsHUDHelper showError:@"不允许重复时，数值范围必须大于或等于生成数量"];
        return;
    }
    SmallDecisionsSjsViewController *sjsVC = [[SmallDecisionsSjsViewController alloc] init];
    sjsVC.min = min;
    sjsVC.max = max;
    sjsVC.count = count;
    sjsVC.allowDuplicates = allowDuplicates;
    [SmallDecisionsNavigatorManager pushViewController:sjsVC];
}

@end
